package cmdIO;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Scanner;

import libraryClass.Book;
import libraryClass.Library;
import userclass.Administrator;
import userclass.Student;
import userclass.User;
import userclass.UserUnit;

public class Menu {

    public User user;
    Scanner in;
    PrintWriter out;


    public Menu(){
        this.in = new Scanner(System.in);
        this.out = new PrintWriter(System.out, true);
    }

    public Menu(InputStream inStream, OutputStream outStream ) {
        this.in = new Scanner(inStream);
        this.out = new PrintWriter(outStream, true);
    }


    public Student register() {
        out.println("请输入您的注册信息：");

        out.println("学生姓名:\t");
        String name=in.next();

        out.println("学号:\t\t");
        long ID;
        try {
            ID=Long.parseLong(in.next()) ;
        }catch(NumberFormatException e) {
            out.println("请重新输入一整行正确的学号");
            out.println(e);
            return register();
        }

        out.println("登录密码:\t");
        String password=in.next();
//		out.println(password);

        Student s=new Student(name, ID, password);
        if(UserUnit.isContain(s)) {
            out.println("很遗憾注册失败：该学号账户已存在。请重新注册");
            return register();
        }

        UserUnit.add(s);
        user=s;
        out.println("恭喜您注册成功，两秒后自动为您登录...");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            out.println(e+"\n\n");
            e.printStackTrace();
        }
        run();
        return  s;

    }

    public User login() {
        out.println("请输入您的登录信息：");

        out.println("学号:\t\t");

        long ID;
        try {
            ID=Long.parseLong(in.next()) ;
        }catch(NumberFormatException e) {
            out.println("请重新输入一整行正确的学号");
            out.println(e);
            return login();
        }

        out.println("登录密码:\t");
//		sc.nextLine();
        String password=in.next();		//next不吃回车nextList吃回车
//		out.println(ID);
//		out.println(password);


        if(UserUnit.peekUser(ID)==null) {
            out.println("很遗憾登录失败：账户不存在。请重新登录");
            return login();
        }
        if(!UserUnit.peekUser(ID).getPassword().equals(password)) {
            out.println("很遗憾登录失败：密码错误。请重新登录");
            return login();
        }

        user=UserUnit.peekUser(ID);
        out.println("恭喜您登录成功");
        run();

        return user;
    }

    public  void run() {


        if(user==null) {
            out.println("菜单显示错误：请先登录");
            return;
        }


        if(user.getID()==0)
            admMenu();

        else
            stuMenu();
    }

    void loginMenu() {

//        try {
//            UserUnit.load("UserUnit");
//        } catch (ClassNotFoundException | IOException e) {
//            e.printStackTrace();
//            UserUnit.initializeAdministrator();
//            out.println("不好意思加载失败，用户数据丢失，回车键继续");
//            in.nextLine();
//
//        }
//        try {
//            Library.load("Library.txt");
//        } catch (ClassNotFoundException | IOException e) {
//            e.printStackTrace();
//            Library.initializeBookStore();
//            out.println("不好意思加载失败，图书储藏信息丢失，回车键继续");
//            in.nextLine();
//
//        }

        boolean flag=false;
        while(!flag) {

            out.println("\n\t欢迎来到图书馆借阅系统");
            out.println("以下是登录选项，请输入序号进行相应的操作\n");
            out.println("\t1.注册账户");
            out.println("\t2.登录系统");
            out.println("\t3.退出系统");

            switch(in.nextInt()) {
                case 1:		register();		break;
                case 2:		login();		break;
                case 3:		out.println("已退出图书馆借阅系统...");		flag=true;		break;
                default:	out.println("请输入正确的操作序号...");		in.nextLine();in.nextLine();	break;
//				case 4:Library.display();

            }

            if(!flag) {
                UserUnit u=new UserUnit();
                try {
                    u.save();
                } catch (IOException e) {
                    e.printStackTrace();
                    out.println("不好意思保存失败，用户数据丢失\n但是退出成功了");
                }

                Library l=new Library();
                try {
                    l.save();
                } catch (IOException e) {
                    e.printStackTrace();
                    out.println("不好意思保存失败，图书储藏信息丢失\n但是退出成功了");
                }
            }
        }
    }

    private void stuMenu(){
        Student stu=(Student)user;
        boolean flag=false;

        while(!flag) {

            out.println("\n\t你好，"+user.getName()+"("+user.getID()+")\n" );
            out.println("以下是您的使用菜单，请输入序号进行相应的操作\n");
            out.println("\t1.现存藏书展览");
            out.println("\t2.借阅图书");
            out.println("\t3.归还图书");
            out.println("\t4.借阅查询");
            out.println("\t5.修改个人信息");
            out.println("\t6.退出登录");

            switch(in.nextInt()) {
                case 1:
                    Library.display( out);
                    out.println("\n\t...按回车键返回上一级菜单");
                    in.nextLine();in.nextLine();
                    break;
                case 2:
                    Library.display(out);
                    out.println("\t请输入所借阅图书的ID：");
                    Book book=stu.borrow(in.nextLong());
                    if(book==null)
                        out.println("很遗憾借书失败:所借书籍不存在或库存为零");
//				else if(stu.getBookLoan().containsKey(book))		//逻辑错误：先添加再判断
//					out.println("已为您更新借阅时间为： "+stu.getBookLoan().get(book).getDate()+" 请牢记要及时归还");
                    else
                        out.println("恭喜您借书成功：您的借阅时间为：  "+stu.getBookLoan().get(book).getDate()+"  请于  "+stu.getBookLoan().get(book).getDeadLine()+"  前归还");

                    out.println("\n\t...按回车键返回上一级菜单");
                    in.nextLine();in.nextLine();
                    break;

                case 3:
                    out.println("\t请输入所归还阅图书的ID：");
                    Book book1=stu.giveback(in.nextLong());
                    if(book1==null)
                        out.println("很遗憾归还失败:您还未有此本书籍");
                    else
                        out.println("恭喜您归还成功");

                    out.println("\n\t...按回车键返回上一级菜单");
                    in.nextLine();in.nextLine();
                    break;

                case 4:
                    stu.displayloan(out);
                    out.println("\n\t...按回车键返回上一级菜单");
                    in.nextLine();in.nextLine();
                    break;

                case 5:
                    break;
                case 6:
                    flag=true;
                    out.println("\n\t...两秒后跳转至登录选项");
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        out.println(e+"\n\n");
                        e.printStackTrace();
                    }
                    break;

                default:
                    out.println("请输入正确的操作序号");
                    break;
            }

        }
    }


    void admMenu(){

        Administrator adm=(Administrator)user;
        boolean flag=false;

        while(!flag) {

            out.println("\n\t你好，"+user.getName()+"("+user.getID()+"）\n " );
            out.println("以下是您的使用菜单，请输入序号进行相应的操作");
            out.println("\t1.现存图书展览");
            out.println("\t2.修改书籍信息");	//一同调用remove个add
            out.println("\t3.添加书籍");
            out.println("\t4.移除书籍");
            out.println("\t5.账户及其借阅信息展示");
            out.println("\t6.添加学生账户");
            out.println("\t7.添加管理员账户");
            out.println("\t8.移除账户");
            out.println("\t9.修改个人信息");
            out.println("\t10.退出登录");

            switch(in.nextInt()) {

                case 1:		break;

                case 2:		break;

                case 3:		break;

                case 4:		break;

                case 5:		break;

                case 6:		break;

                case 7:		break;

                case 8:		break;

                case 9:		break;

                case 10:
                    flag=true;
                    out.println("\n\t...两秒后跳转至登录选项");
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        out.println(e+"\n\n");
                        e.printStackTrace();
                    }
                    break;

                default:
                    out.println("请输入正确的操作序号");
                    break;
            }
        }

    }

}

